package com.seocoo.worker.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.seocoo.worker.R;
import com.seocoo.worker.base.widget.AppToolBer;

/***
 * 公用web
 */
public class CommWebActivity extends FragmentActivity {
    private static String EXTRA_URL = "extra_url";
    private static String EXTRA_TITLE = "extra_title";
    private AppToolBer appToolBer;
    private WebView fullWebView;


    public static void start(Context context,String title, String url) {
        Intent intent = new Intent(context, CommWebActivity.class);
        intent.putExtra(EXTRA_URL, url);
        intent.putExtra(EXTRA_TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.com_web_activity);
        appToolBer = (AppToolBer) findViewById(R.id.appToolBer);
        fullWebView = (WebView) findViewById(R.id.fullWebView);

        WebSettings webSetting = this.fullWebView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        webSetting.setAppCacheEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSetting.setDefaultTextEncodingName("utf-8");

        String title = getIntent().getStringExtra(EXTRA_TITLE);
        String url = getIntent().getStringExtra(EXTRA_URL);
        if (TextUtils.isEmpty(title)){
            fullWebView.setWebChromeClient(new WebChromeClient(){
                @Override
                public void onReceivedTitle(WebView view, String title) {
                    super.onReceivedTitle(view, title);
                    appToolBer.getTvTitle().setText(title);
                }
            });
        }else {
            appToolBer.getTvTitle().setText(title);
        }
        fullWebView.loadUrl(url);

    }
    @Override
    public void finish() {
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
        super.finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        fullWebView.destroy();
    }
}
